package br.com.fsa.aplication.Services;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.fsa.aplication.Database.UsuarioDAO;
import br.com.fsa.aplication.Exception.ConectionException;
import br.com.fsa.aplication.Model.UsuarioVO;
import br.com.fsa.aplication.Model.bean.Usuario;
import br.com.fsa.aplication.Util.PropertiesWS;

public class LoginSCN
{

	public boolean executaLogin(UsuarioVO _login) throws ConectionException
	{
		PropertiesWS prop = new PropertiesWS();
		String pUsuario = prop.buscaValor("USUARIO");
		String pSenha = prop.buscaValor("SENHA");
		String pNome = prop.buscaValor("NOME"); 
		if (_login.getUsuario().toUpperCase().equals(pUsuario.toUpperCase())
				&& _login.getSenha().toUpperCase().equals(pSenha.toUpperCase()))
		{
			_login.setNome(pNome);
			HttpSession session = (HttpSession) FacesContext
					.getCurrentInstance().getExternalContext()
					.getSession(false);
			session.setAttribute("USUARIO", _login);
			return true;
		}

		UsuarioDAO usuDao = new UsuarioDAO();

		Usuario usu =   usuDao.findUsuario(_login.getUsuario(), _login.getSenha());
		if (usu != null )
		{
			_login.setNome(usu.getNome());
			_login.setUsuarioID(usu.getUsuarioID());
			
			HttpSession session = (HttpSession) FacesContext
					.getCurrentInstance().getExternalContext()
					.getSession(false);
			session.setAttribute("USUARIO", _login);
			return true;
		}

		return false;
	}
}
